home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / Immunix / Repository.pm < prev    next >
Encoding:
Perl POD Document  |  2009-04-08  |  10.0 KB  |  355 lines

  1. # ----------------------------------------------------------------------
  2. #    Copyright (c) 2008 Dominic Reynolds
  3. #
  4. #    This program is free software; you can redistribute it and/or
  5. #    modify it under the terms of version 2 of the GNU General Public
  6. #    License as published by the Free Software Foundation.
  7. #
  8. #    This program is distributed in the hope that it will be useful,
  9. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. #    GNU General Public License for more details.
  12. #
  13. #
  14. # ----------------------------------------------------------------------
  15.  
  16. package Immunix::Repository;
  17.  
  18. use strict;
  19. use warnings;
  20.  
  21. use Carp;
  22. use Cwd qw(cwd realpath);
  23. use Data::Dumper;
  24. use File::Basename;
  25. use File::Temp qw/ tempfile tempdir /;
  26. use Immunix::Config;
  27. use Locale::gettext;
  28. use POSIX;
  29. use RPC::XML;
  30. use RPC::XML::Client;
  31.  
  32.  
  33. require Exporter;
  34. our @ISA    = qw(Exporter);
  35. our @EXPORT = qw(
  36.     get_repo_client
  37.     did_result_succeed
  38.     get_result_error
  39.     user_login
  40.     user_register
  41.     upload_profile
  42.     fetch_profile_by_id
  43.     fetch_profiles_by_user
  44.     fetch_profiles_by_name
  45.     fetch_profiles_by_name_and_user
  46.     fetch_newer_profile
  47.     get_repo_config
  48.     set_repo_config
  49. );
  50.  
  51. our %clients;
  52. our %uid2login;
  53. our $DEBUGGING = 0;
  54. our $repo_cfg;
  55. our $aa_cfg;
  56.  
  57. sub get_repo_client ($) {
  58.     my $repo_url = shift;
  59.     unless ( $clients{$repo_url} ) {
  60.         $clients{$repo_url} = new RPC::XML::Client $repo_url;
  61.     }
  62.     return $clients{$repo_url};
  63. }
  64.  
  65. sub did_result_succeed {
  66.     my $result = shift;
  67.  
  68.     my $ref = ref $result;
  69.     return ($ref && $ref ne "RPC::XML::fault") ? 1 : 0;
  70. }
  71.  
  72. sub get_result_error {
  73.     my $result = shift;
  74.  
  75.     if (ref $result) {
  76.         if (ref $result eq "RPC::XML::fault") {
  77.             $result = $result->string;
  78.         } else {
  79.             $result = $$result;
  80.         }
  81.     }
  82.     return $result;
  83. }
  84.  
  85. sub user_login ($$$) {
  86.     my ($repo_url,$user,$pass) = @_;
  87.     my ($status,$detail);
  88.     my $repo_client = get_repo_client( $repo_url );
  89.     if ( $repo_client ) {
  90.         my $res = $repo_client->send_request('LoginConfirm', $user, $pass);
  91.         if (did_result_succeed($res)) {
  92.             $status = 1;
  93.             $detail = "";
  94.         } else {
  95.             $status = 0;
  96.             $detail = get_result_error($res);
  97.         }
  98.     }
  99.     return $status,$detail;
  100. }
  101.  
  102.  
  103. sub user_register ($$$$) {
  104.     my ($repo_url,$user,$pass,$email) = @_;
  105.     my $repo_client = get_repo_client( $repo_url );
  106.     my ($status,$detail);
  107.     if ( $repo_client ) {
  108.         my $res = $repo_client->send_request('Signup', $user, $pass, $email);
  109.         if (did_result_succeed($res)) {
  110.             $status = 1;
  111.             $detail = "";
  112.         } else {
  113.             $status  = 0;
  114.             $detail = get_result_error($res);
  115.         }
  116.     }
  117.     return $status,$detail;
  118. }
  119.  
  120. sub upload_profile ($$$$$$$) {
  121.     my ($repo_url,$user,$pass,$distro,$pname,$profile,$changelog) = @_;
  122.     my ($status,$detail);
  123.     my $repo_client = get_repo_client( $repo_url );
  124.     my $res = $repo_client->send_request( 'Create', $user, $pass, $distro,
  125.                                           $pname, $profile, $changelog);
  126.     if (did_result_succeed($res)) {
  127.        $detail = $res->value;
  128.        $status  = 1;
  129.     } else {
  130.        $detail = get_result_error($res);
  131.        $status  = 0;
  132.     }
  133.     return $status,$detail;
  134. }
  135.  
  136. sub fetch_profile_by_id ($$) {
  137.     my ($repo_url,$id) = @_;
  138.     my $repo_client = get_repo_client( $repo_url );
  139.     my $repo_profile;
  140.     my ($status,$detail);
  141.     my $res = $repo_client->send_request('Show', $id);
  142.     if (did_result_succeed($res)) {
  143.         $status = 1;
  144.         $detail = $res->value();
  145.     } else {
  146.         $status  = 0;
  147.         $detail = get_result_error($res);
  148.     }
  149.  
  150.     return $status, $detail;
  151. }
  152.  
  153.  
  154. sub fetch_profiles ($$$$) {
  155.     my ($repo_url,$distro,$username,$fqdn) = @_;
  156.     my $p_hash = {};
  157.     my ($status,$detail);
  158.     my $repo_client = get_repo_client( $repo_url );
  159.     my $res =
  160.       $repo_client->send_request('FindProfiles', $distro, $fqdn, $username);
  161.     if (did_result_succeed($res)) {
  162.         $status = 1;
  163.         for my $p ( @$res ) {
  164.             my $p_repo = $p->{profile}->value();
  165.             $p_repo =~ s/flags=\(complain\)// if ( $p_repo );  #strip complain flag
  166.             $p->{profile}             = $p_repo;
  167.             $p->{user_id}             = $p->{user_id}->value();
  168.             $p->{id}                  = $p->{id}->value();
  169.             $p->{name}                = $p->{name}->value();
  170.             $p->{created_at}          = $p->{created_at}->value();
  171.             $p->{downloaded_count}    = $p->{downloaded_count}->value();
  172.         }
  173.         $detail = $res;
  174.     } else {
  175.         $status = 0;
  176.         $detail = get_result_error($res);
  177.     }
  178.     return $status,$detail;
  179. }
  180.  
  181. sub fetch_profiles_by_user ($$$) {
  182.     my ($repo_url,$distro,$username) = @_;
  183.     my $p_hash = {};
  184.     my ($status,$detail) = fetch_profiles( $repo_url, $distro, $username, "" );
  185.     if ( $status ) {
  186.         for my $p ( @$detail ) {
  187.             my $p_repo = $p->{profile};
  188.             if ($p_repo ne "") {
  189.                 $p->{username} = $username;
  190.                 $p_hash->{$p->{name}} = $p;
  191.             }
  192.         }
  193.     } else {
  194.         return ($status,$detail);
  195.     }
  196.     return($status,$p_hash);
  197. }
  198.  
  199.  
  200. sub fetch_profiles_by_name_and_user ($$$$) {
  201.     my ($repo_url,$distro,$fqdbin, $username) = @_;
  202.     my $p_hash = {};
  203.     my ($status,$detail) = fetch_profiles( $repo_url, $distro, $username, $fqdbin );
  204.     if ( $status ) {
  205.         for my $p ( @$detail ) {
  206.             my $p_repo = $p->{profile}?$p->{profile}:"";
  207.             $p_hash->{$p->{name}} = $p if ($p_repo ne "");
  208.         }
  209.     } else {
  210.         return ($status,$detail);
  211.     }
  212.     return($status,$p_hash);
  213. }
  214.  
  215.  
  216. sub fetch_profiles_by_name ($$$) {
  217.     my ($repo_url,$distro,$fqdbin) = @_;
  218.     my ($status,$detail,$data);
  219.     $detail = {};
  220.     ($status,$data) = fetch_profiles( $repo_url, $distro, "", $fqdbin);
  221.     if ($status) {
  222.         my @uids;
  223.         for my $p (@$data) {
  224.             push @uids, $p->{user_id};
  225.         }
  226.         my ($status_unames,$unames) = fetch_usernames_from_uids($repo_url, @uids);
  227.         if ( $status_unames ) {
  228.             for my $p (@$data) {
  229.                 if ( $unames->{$p->{user_id}} ) {
  230.                     $p->{username} = $unames->{$p->{user_id}};
  231.                 } else {
  232.                     $p->{username} = "unkown-" . $p->{user_id};
  233.                 }
  234.             }
  235.  
  236.         } else {
  237.             print STDOUT "ERROR UID\n";
  238.         }
  239.         for my $p (@$data) {
  240.             $p->{profile_type} = "REPOSITORY";
  241.             $detail->{$p->{username}} = $p;
  242.         }
  243.     } else {
  244.         $detail = $data;
  245.     }
  246.     return $status,$detail;
  247. }
  248.  
  249.  
  250. sub fetch_newer_profile ($$$$$) {
  251.     my ($repo_url,$distro,$user,$id,$profile)  = @_;
  252.     my $repo_client = get_repo_client( $repo_url );
  253.     my $p;
  254.     my ($status,$detail);
  255.  
  256.     if ($repo_client) {
  257.         my $res =
  258.           $repo_client->send_request('FindProfiles', $distro, $profile, $user);
  259.         if (did_result_succeed($res)) {
  260.             my @profiles;
  261.             my @profile_list = @{$res->value};
  262.             $status = 1;
  263.  
  264.             if (@profile_list) {
  265.                 if ($profile_list[0]->{id} > $id) {
  266.                     $p = $profile_list[0];
  267.                 }
  268.             }
  269.             $detail = $p;
  270.         } else {
  271.             $status = 0;
  272.             $detail = get_result_error($res);
  273.         }
  274.     }
  275.     return $status,$detail;
  276. }
  277.  
  278. sub fetch_usernames_from_uids ($) {
  279.     my ($repo_url,@searchuids) = @_;
  280.     my ($status,$result) = (1,{});
  281.     my @uids;
  282.  
  283.     for my $uid ( @searchuids ) {
  284.         if ( $uid2login{$uid} ) {
  285.             $result->{$uid} = $uid2login{$uid};
  286.         } else {
  287.             push @uids, $uid;
  288.         }
  289.     }
  290.     if (@uids) {
  291.         my $repo_client = get_repo_client( $repo_url );
  292.     #RPC::XML will serialize the array into XML with the is_utf8 flag set
  293.     #which causes, HTTP:Message to fail.  Looping on the array elements
  294.     #stops this from happening, and since these are all numbers it
  295.     #will not cause problems.
  296.     for my $foo (@uids) {
  297.         Encode::_utf8_off($foo);
  298.     }
  299.         my $res = $repo_client->send_request('LoginNamesFromUserIds', [@uids]);
  300.         if (did_result_succeed($res)) {
  301.             my @usernames = @{ $res->value };
  302.             for my $uid (@uids) {
  303.                 my $username = shift @usernames;
  304.                 $uid2login{$uid} = $username;
  305.                 $result->{$uid} = $uid2login{$uid};
  306.             }
  307.         } else {
  308.             $status = 0;
  309.             $result = get_result_error($res);
  310.         }
  311.     }
  312.     return $status,$result;
  313. }
  314.  
  315. sub get_repo_config {
  316.     unless ( $repo_cfg ) {
  317.        $repo_cfg = Immunix::Config::read_config("repository.conf");
  318.     }
  319.     unless ( $aa_cfg ) {
  320.        $aa_cfg = Immunix::Config::read_config("logprof.conf");
  321.     }
  322.     return {
  323.               "url"      => $aa_cfg->{repository}{url},
  324.               "distro"   => $aa_cfg->{repository}{distro},
  325.               "enabled"  => $repo_cfg->{repository}{enabled},
  326.               "upload"   => $repo_cfg->{repository}{upload},
  327.               "user"     => $repo_cfg->{repository}{user},
  328.               "password" => $repo_cfg->{repository}{pass},
  329.               "email"    => $repo_cfg->{repository}{email}
  330.             };
  331. }
  332.  
  333. sub set_repo_config ($) {
  334.     my $cfg = shift;
  335.     my ($url,$distro,$enabled,$upload,$user,$pass);
  336.     unless ( $repo_cfg ) {
  337.        $repo_cfg = Immunix::Config::read_config("repository.conf");
  338.     }
  339.     unless ( $aa_cfg ) {
  340.        $aa_cfg = Immunix::Config::read_config("logprof.conf");
  341.     }
  342.     $repo_cfg->{repository}{enabled} = $cfg->{enabled} if ( $cfg->{enabled} );
  343.     $repo_cfg->{repository}{upload}  = $cfg->{upload}  if ( $cfg->{upload} );
  344.     $repo_cfg->{repository}{user}    = $cfg->{user}    if ( $cfg->{user} );
  345.     $repo_cfg->{repository}{pass}    = $cfg->{password}if ( $cfg->{password} );
  346.     $repo_cfg->{repository}{email}   = $cfg->{email}   if ( $cfg->{email} );
  347.     $aa_cfg->{repository}{distro}    = $cfg->{distro}  if ( $cfg->{distro} );
  348.     $aa_cfg->{repository}{url}       = $cfg->{url}     if ( $cfg->{url} );
  349.     write_config("repository.conf", $repo_cfg);
  350.     write_config("logprof.conf", $aa_cfg);
  351. }
  352.  
  353.  
  354. 1;
  355.